//找到edit（+-的名字）元素
let edit = document.getElementsByClassName('edit')
//遍历edit数组中的元素
for (let i = 0; i < edit.length; i++) {
    //edit点击事件
    edit[i].onclick = function () {
        //找到单价，父级元素的上一个元素，innerHTML改变其内容
        let price = this.parentNode.previousElementSibling.innerHTML
        //截取单价数组第二个开始，转换成数值型，可以运算
        price = parseFloat(price.substring(1))
        //获得- +属性
        let op = this.getAttribute('data-op')
        //num是+ -中间的数字，找到它并改变它
        let num
        if (op === '+') {
            //找到num并改变 num=edit的+的上一个元素
            num = parseInt(this.previousElementSibling.innerHTML)
            num++
            //如果点击是+，num++,重新赋值给num
            this.previousElementSibling.innerHTML = num
        } else if (op === '-') {
            //找到num，-的下一个元素改变它
            num = parseInt(this.nextElementSibling.innerHTML)
            num--
            //判断如果num不能为0
            if (num === 0) {
                alert('商品数量必须大于0')
                num = 1
            }
            //如果点击是-，num--,重新赋值给num
            this.nextElementSibling.innerHTML = num
        }
        //sumprice是单行总价，拼接字符串，数值乘单价，保留一位小数
        let sumprice = '$' + (num * price).toFixed(1)
        //找到sum-price
        let sumPrice = document.getElementsByClassName('sum-price')
        //改变其内容，总价为edit的父元素的下一个元素
        this.parentNode.nextElementSibling.innerHTML = sumprice;
        //total为总价
        let total = 0
        //遍历total的内容
        for (let i = 0; i < sumPrice.length; i++) {
            //总价为数组中每一位价格的相加
            total += parseFloat(sumPrice[i].innerHTML.substring(1))
        }
        //拼接字符串
        total = '$' + total.toFixed(1) + '元';
        let Sum = document.getElementsByClassName('sum')[0];
        Sum.innerHTML = total

    }
}